import '@progress/kendo-ui/src/kendo.tabstrip.js';

let dom;

describe("tabstrip data binding", function() {
    beforeEach(function() {
        dom = $("<ul>");
    });
    afterEach(function() {
        kendo.destroy(dom);
    });

    it("binding to data source", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataSource: [{}, {}]
        });

        assert.equal(dom.find("li").length, 2);
    });

    it("the dataSource field is instance of kendo.data.DataSource", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataSource: [{}, {}]
        });

        assert.isOk(tabstrip.dataSource instanceof kendo.data.DataSource, "dataSource is instanceof kendo.data.DataSource");
    });

    it("binding to array of primitive types", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataSource: ["foo"]
        });

        assert.equal(dom.find("li").text(), "foo");
    });

    it("binding to array of complex objects displays [object Object] by default", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataSource: [{}]
        });

        assert.equal(dom.find("li").text(), "[object Object]");
    });

    it("binding to array of complex objects displays the dataTextField", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataTextField: "foo",
            dataSource: [{ foo: "foo" }]
        });

        assert.equal(dom.find("li").text(), "foo");
    });

    it("dataTextField as array access expression", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataTextField: "['foo']",
            dataSource: [{ foo: "foo" }]
        });

        assert.equal(dom.find("li").text(), "foo");
    });

    it("binding raises the dataBinding event", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataBinding: function() {
                assert.isOk(true, "Event is raised");
            },
            dataSource: [{}]
        });
    });

    it("items are updated on change event of the dataSource", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataTextField: "foo",
            dataUrlField: "bar",
            dataSource: [{ foo: "foo", bar: "index.html" }]
        });

        assert.equal(dom.find("li").text(), "foo");

        tabstrip.dataSource.view()[0].set("bar", "newvalue");

        assert.equal(tabstrip._contentUrls[0], "newvalue");
    });

    it("binding raises the dataBound event", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataBound: function() {
                assert.isOk(true, "Event is raised");
            },
            dataSource: [{}]
        });
    });

    it("dataContentField", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataContentField: "foo",
            dataSource: [{ foo: "foo" }]
        });

        assert.equal(tabstrip.contentElements.first().text(), "foo");
    });

    it("dataImageUrlField", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataImageUrlField: "foo",
            dataSource: [{ foo: "http://example.com" }]
        });

        assert.equal(tabstrip.tabGroup.find("img").attr("src"), "http://example.com");
    });

    it("dataUrlField", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataUrlField: "foo",
            dataSource: [{ foo: "http://example.com/" }]
        });

        assert.equal(tabstrip.tabGroup.find("a").attr("href"), "http://example.com/");
    });

    it("dataSpriteCssClass", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataSpriteCssClass: "foo",
            dataSource: [{ foo: "foo" }]
        });

        assert.isOk(tabstrip.tabGroup.find("span.k-sprite").hasClass("foo"));
    });

    it("resetting dataSource detaches the previous events", function() {
        let tabstrip = new kendo.ui.TabStrip(dom);

        let dataSource = tabstrip.dataSource;

        let called = false;

        tabstrip._dataSource();

        tabstrip.bind("dataBound", function() {
            called = true;
        });

        dataSource.read();

        assert.isOk(!called, "Change event is not detached");
    });

    it("resetting DataSource rebinds the widget and refreshes content elements", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataContentField: "content"
        });

        tabstrip.setDataSource(new kendo.data.DataSource({
            data: [{ text: 1, value: 1, content: "1" }, { text: 2, value: 2, content: "2" }]
        }));

        assert.equal(tabstrip.wrapper.find("li").length, 2);
        assert.equal(tabstrip.wrapper.children("div:not(.k-tabstrip-items-wrapper)").length, 2);
    });

    it("adding item to empty dataSource adds a tab without errors", function() {
        let tabstrip = new kendo.ui.TabStrip(dom, {
            dataTextField: "Name",
            dataSource: []
        });

        tabstrip.dataSource.add({ Name: "Tab2" });

        assert.equal(dom.find("li").length, 1);
        assert.equal(dom.find("li").text(), "Tab2");
    });
});
